home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / src / portable / overlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  8.5 KB  |  277 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. #define    CURSES_LIBRARY    1
  20. #include <curses.h>
  21.  
  22. /* undefine any macros for functions defined in this module */
  23. #undef    overlay
  24. #undef    overwrite
  25. #undef    copywin
  26.  
  27. /* undefine any macros for functions called by this module if in debug mode */
  28. #ifdef PDCDEBUG
  29. #  undef    move
  30. #  undef    wmove
  31. #endif
  32.  
  33. #ifdef PDCDEBUG
  34. char *rcsid_overlay  = "$Id$";
  35. #endif
  36.  
  37. /*man-start*********************************************************************
  38.  
  39.   Name:                                                       overlay
  40.  
  41.   Synopsis:
  42.       int    overlay(WINDOW *src_w, WINDOW *dst_w)
  43.       int    overwrite(WINDOW *src_w, WINDOW *dst_w)
  44.       int    copywin(WINDOW *src_w, WINDOW *dst_w, int src_tr,
  45.               int src_tc, int dst_tr, int dst_tc, int dst_br, int dst_bc,
  46.               bool overlay)
  47.  
  48.   X/Open Description:
  49.      The overlay() and overwrite() functions overlay src_w on top of 
  50.      dst_w; that is, all text in src_w is copied into dst_w. The windows
  51.      src_w and dst_w are not required to be the same size. The
  52.      copy starts at (0, 0) on each window. The difference between
  53.      the two functions is that overlay() is non-destructive
  54.      (blanks are not copied) while overwrite() is destructive
  55.      (blanks are copied).
  56.  
  57.      copywin() is similar to overwrite() and overlay() but copywin()
  58.      does not require that the two windows overlap.
  59.      The arguments src_tc and src_tr specify the top left corner of the
  60.      region to be copied to the destination window.
  61.      The arguments dst_tc,dst_tr,dst_br,dst_bc specify the region within
  62.      the destination window to where the copy is made.
  63.      The argument overlay, if TRUE, indicates that the copy is done
  64.      non-destructively (as in overlay()). Blanks in the source window
  65.      are not copied to the destination window. When overlay is FALSE,
  66.      (as in overwrit()), the copy is destructive; blanks are copied
  67.      to the destination window.
  68.  
  69.   PDCurses Description:
  70.      The above description for overlay() and overwrite() is misleading 
  71.      in the actual behaviour exhibited by both SysV and BSD curses. 
  72.      The above implies that the character in 0,0 of the source window 
  73.      is copied to 0,0 of the destination window. What actually happens 
  74.      is that those characters in the source window that intersect with 
  75.      characters in the destination window RELATIVE TO ABSOLUTE 0,0 ON 
  76.      THE SCREEN, are copied to the destination window so that the 
  77.      characters appear in the same physical position on the screen.
  78.  
  79.      Thanks to Andreas Otte (venn@@uni-paderborn.de) for the correction
  80.      and code changes.
  81.  
  82.   X/Open Return Value:
  83.      All functions return OK on success and ERR on error.
  84.  
  85.   X/Open Errors:
  86.      No errors are defined for this function.
  87.  
  88.   Portability                             X/Open    BSD    SYS V
  89.                                           Dec '88
  90.       overlay                               Y        Y       Y
  91.       overwrite                             Y        Y       Y
  92.       copywin                               -        -      3.0
  93.  
  94. **man-end**********************************************************************/
  95.  
  96. /***********************************************************************/
  97. int    overlay(WINDOW *src_w, WINDOW *dst_w)
  98. /***********************************************************************/
  99. {
  100.     int    last_line;
  101.     int    last_col;
  102.     int    first_line;
  103.     int    first_col;
  104.     int    src_start_x;
  105.     int    src_start_y;
  106.     int    dst_start_x;
  107.     int    dst_start_y;
  108.     int    xdiff;
  109.     int    ydiff;
  110.     int    rc;
  111.  
  112. #ifdef PDCDEBUG
  113.     if (trace_on) PDC_debug("overlay() - called\n");
  114. #endif
  115.  
  116.     if (src_w == (WINDOW *)NULL)    return( ERR );
  117.     if (dst_w == (WINDOW *)NULL)    return( ERR );
  118.  
  119.     first_col  = max(dst_w->_begx,src_w->_begx);
  120.     first_line = max(dst_w->_begy,src_w->_begy);
  121.     last_col   = min(src_w->_begx+src_w->_maxx, dst_w->_begx+dst_w->_maxx);
  122.     last_line  = min(src_w->_begy+src_w->_maxy, dst_w->_begy+dst_w->_maxy);
  123.  
  124. /* determine the overlapping region of the two windows in real coordinates */
  125.     if ((last_col < first_col) || (last_line < first_line))
  126.     return(OK);  /* if no overlapping region, do nothing */
  127.  
  128. /* size of overlapping region */
  129.     xdiff = last_col - first_col;
  130.     ydiff = last_line - first_line;
  131.  
  132.     if (src_w->_begx <= dst_w->_begx)
  133.         {
  134.         src_start_x = dst_w->_begx - src_w->_begx;
  135.         dst_start_x = 0;
  136.         }
  137.     else
  138.         {
  139.         dst_start_x = src_w->_begx - dst_w->_begx;
  140.         src_start_x = 0;
  141.         }
  142.     if (src_w->_begy <= dst_w->_begy)
  143.         {
  144.         src_start_y = dst_w->_begy - src_w->_begy;
  145.         dst_start_y = 0;
  146.         }
  147.     else
  148.         {
  149.         dst_start_y = src_w->_begy - dst_w->_begy;
  150.         src_start_y = 0;
  151.         }
  152.  
  153.     rc = PDC_copy_win(src_w,dst_w,src_start_y,src_start_x,
  154.         src_start_y+ydiff,src_start_x+xdiff,dst_start_y,dst_start_x,
  155.         dst_start_y+ydiff,dst_start_x+xdiff,TRUE);
  156.  
  157.     return( rc );
  158. }
  159. /***********************************************************************/
  160. int    overwrite(WINDOW *src_w, WINDOW *dst_w)
  161. /***********************************************************************/
  162. {
  163.     int    last_line;
  164.     int    last_col;
  165.     int    first_line;
  166.     int    first_col;
  167.     int    src_start_x;
  168.     int    src_start_y;
  169.     int    dst_start_x;
  170.     int    dst_start_y;
  171.     int    xdiff;
  172.     int    ydiff;
  173.     int    rc;
  174.  
  175. #ifdef PDCDEBUG
  176.     if (trace_on) PDC_debug("overwrit() - called\n");
  177. #endif
  178.  
  179.     if (src_w == (WINDOW *)NULL)    return( ERR );
  180.     if (dst_w == (WINDOW *)NULL)    return( ERR );
  181.  
  182.     first_col  = max(dst_w->_begx,src_w->_begx);
  183.     first_line = max(dst_w->_begy,src_w->_begy);
  184.     last_col   = min(src_w->_begx+src_w->_maxx, dst_w->_begx+dst_w->_maxx);
  185.     last_line  = min(src_w->_begy+src_w->_maxy, dst_w->_begy+dst_w->_maxy);
  186.  
  187. /* determine the overlapping region of the two windows in real coordinates */
  188.     if ((last_col < first_col) || (last_line < first_line))
  189.     return(OK);  /* if no overlapping region, do nothing */
  190.  
  191. /* size of overlapping region */
  192.     xdiff = last_col - first_col;
  193.     ydiff = last_line - first_line;
  194.  
  195.     if (src_w->_begx <= dst_w->_begx)
  196.         {
  197.         src_start_x = dst_w->_begx - src_w->_begx;
  198.         dst_start_x = 0;
  199.         }
  200.     else
  201.         {
  202.         dst_start_x = src_w->_begx - dst_w->_begx;
  203.         src_start_x = 0;
  204.         }
  205.     if (src_w->_begy <= dst_w->_begy)
  206.         {
  207.         src_start_y = dst_w->_begy - src_w->_begy;
  208.         dst_start_y = 0;
  209.         }
  210.     else
  211.         {
  212.         dst_start_y = src_w->_begy - dst_w->_begy;
  213.         src_start_y = 0;
  214.         }
  215.  
  216.     rc = PDC_copy_win(src_w,dst_w,src_start_y,src_start_x,
  217.         src_start_y+ydiff,src_start_x+xdiff,dst_start_y,dst_start_x,
  218.         dst_start_y+ydiff,dst_start_x+xdiff,FALSE);
  219.  
  220.     return( rc );
  221. }
  222. /***********************************************************************/
  223. int    copywin(WINDOW *src_w, WINDOW *dst_w, int src_tr,
  224.              int src_tc, int dst_tr, int dst_tc, int dst_br, int dst_bc,
  225.              int overlay)
  226. /***********************************************************************/
  227. {
  228.     int    src_start_x = src_tc;
  229.     int    src_start_y = src_tr;
  230.     int    dst_start_x = dst_tc;
  231.     int    dst_start_y = dst_tr;
  232.     int    src_end_x;
  233.     int    src_end_y;
  234.     int    dst_end_x;
  235.     int    dst_end_y;
  236.     int    src_rows,src_cols;
  237.     int    dst_rows,dst_cols;
  238.     int    min_rows,min_cols;
  239.     int    max_rows,max_cols;
  240.     int    rc;
  241.  
  242. #ifdef PDCDEBUG
  243.     if (trace_on) PDC_debug("copywin() - called\n");
  244. #endif
  245.  
  246.     if (src_w == (WINDOW *)NULL
  247.     ||  dst_w == (WINDOW *)NULL)
  248.         return( ERR );
  249.  
  250.     if (dst_w == curscr)    return( ERR );
  251.  
  252.     if (dst_br >= dst_w->_maxy
  253.     ||  dst_bc >= dst_w->_maxx
  254.     ||  dst_tr < 0
  255.     ||  dst_tc < 0)
  256.         return( ERR );
  257.  
  258.     src_rows = src_w->_maxy - src_tr;
  259.     src_cols = src_w->_maxx - src_tc;
  260.     dst_rows = dst_br - dst_tr;
  261.     dst_cols = dst_bc - dst_tc;
  262.  
  263.     min_rows = min(src_rows,dst_rows);
  264.     min_cols = min(src_cols,dst_cols);
  265.  
  266.     src_end_y = src_tr + min_rows;
  267.     src_end_x = src_tc + min_cols;
  268.     dst_end_y = dst_tr + min_rows;
  269.     dst_end_x = dst_tc + min_cols;
  270.  
  271.     rc = PDC_copy_win(src_w,dst_w,src_start_y,src_start_x,src_end_y,src_end_x,
  272.         dst_start_y,dst_start_x,dst_end_y,dst_end_x,overlay);
  273.  
  274.     return( rc );
  275. }
  276.